<script setup>
import { ref } from "vue";
import { Edit, Search, Delete } from "@element-plus/icons-vue";
import axios from "@/util/axios";
import Details from "./components/details.vue";
import imageDetails from "./components/imageDetails.vue";
import { ElMessage, ElMessageBox } from 'element-plus'


const queryForm = ref({
  query: "",
  pageNum:1,
  pageSize:10
});

const total = ref(0)

const tableData = ref([])

const id = ref(-1)

 const detailsTitle = ref('')

 const detailsVisible = ref(false)

 const imageDetailsVisible = ref(false)

 const imageDetailsValue = ref({})


const initFruitTypeList = async()=>{
    const res = await axios.post("admin/fruitType/list",queryForm.value)
    tableData.value=res.data.fruitTypeList;
    total.value=res.data.total;
}

initFruitTypeList();

// 实现页数
const handleSizeChange=(pageSize)=>{
    queryForm.value.pageNum=1;
    queryForm.value.pageSize=pageSize;
    initFruitTypeList();
}
const handleCurrentChange =(pageNum)=>{
    queryForm.value.pageNum = pageNum;
    initFruitTypeList();
}


const handlDetailsValue=(fruitTypeId)=>{
  if(fruitTypeId){
    id.value = fruitTypeId;
    detailsTitle.value="商品大类修改"
  }else{
    id.value = -1;
    detailsTitle.value="商品大类添加"
  }
  detailsVisible.value=true
}


//图片更换,row是传一行的值过去
const handlImageDetailsValue=(row)=>{
  imageDetailsVisible.value = true
  imageDetailsValue.value = JSON.parse(JSON.stringify(row))

}


 //删除功能
 const handlDelete=(id,status)=>{
  ElMessageBox.confirm(
    '是否确定删除订单？',
    '系统提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
  //执行成功
    .then(async() => {
      let res = await axios.get("/admin/fruitType/delete/"+id)
      if(res.data.code==0){
        ElMessage({
        type: 'success',
        message: '删除成功',
      })
      initFruitTypeList();
      }else{
        ElMessage({
        type: 'error',
        message: res.data.msg,
      })
      }
    })
    //遇到异常执行
    .catch(() => {
     
    })
}


</script>

<template>
  <!-- 大类 -->
    <!-- el-card悬浮框 -->
  <el-card>
    <el-row :gutter="20" class="header">
      <el-col :span="7">
        <el-input
          placeholder="请输入搜索内容"
          v-model="queryForm.query"
          clearable
        ></el-input>
      </el-col>
      <el-button type="button" :icon="Search" @click="initFruitTypeList">搜索</el-button>
      <el-button type="success" @click="handlDetailsValue()">添加大类</el-button>

    </el-row>
    <el-table :data="tableData" height="500" style="width: 100%">
      <el-table-column prop="id" label="#id" width="80" />
      <el-table-column prop="name" label="商品大类名称" width="200" />
      <el-table-column prop="image" label="图片" width="200">
        <template v-slot="scope">
                <!-- /image/fiery是后端WebAppConfigurer里写的 -->
                <img :src="'http://localhost:8080/image/fruitType/'+scope.row.image" width="100" height="100" />
            </template>
      </el-table-column>
      <el-table-column prop="remark" label="描述" />

      <el-table-column prop="action" label="操作" width="300" fixed="right">
        <template v-slot="scope">
          <!-- 括号后面是传入什么数据 -->
          <el-button type="warning" @click="handlImageDetailsValue(scope.row)">更换图片</el-button>
          <el-button type="primary" :icon="Edit" @click="handlDetailsValue(scope.row.id)"></el-button>
          <el-button type="danger" :icon="Delete" @click="handlDelete(scope.row.id)"></el-button>

        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      v-model:current-page="queryForm.pageNum"
      v-model:page-size="queryForm.pageSize"
      :page-sizes="[10, 20, 30, 40, 50]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </el-card>
  
  <Details v-model="detailsVisible" :detailsVisible="detailsVisible" :id="id" :details-title="detailsTitle" @initFruitTypeList="initFruitTypeList"></Details>
  <imageDetails v-model="imageDetailsVisible" @initFruitTypeList="initFruitTypeList" :imageDetailsValue="imageDetailsValue"></imageDetails>
</template>

<style scoped>
 .header{
  padding-bottom: 16px;
  box-sizing: border-box;
 }
 .el-pagination{
  padding-top: 15px;
  box-sizing: border-box;
 }
</style>